/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.adapters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import dk.redia.lib.model.InfoObject;
import dk.redia.lib.model.Media;
import dk.redia.lib.net.ImageCache;
import dk.redia.lib.utils.AppApplication;
import dk.redia.lib.utils.AppConfiguration;
import dk.redia.lib.utils.DateUtils;
import dk.redia.libraryapp.R;
import dk.redia.libraryapp.utils.ResourceUtils;
import dk.redia.libraryapp.viewutils.ViewUtils;

public class ItHappensAdapter implements ListAdapter {

	private List<InfoObject> infoObjects;
	private DataSetObserverCollection observers;
	private Context context;
	
	public ItHappensAdapter(Context context) {
		this.context = context;
		this.observers = new DataSetObserverCollection();
		this.infoObjects = new ArrayList<InfoObject>();
	}
	
	public int getCount() {
		return infoObjects.size();
	}

	public Object getItem(int position) {
		return infoObjects.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (convertView==null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.ithappensitem, null);
		}
		
		InfoObject io = infoObjects.get(position);
		v.setTag(io);
		v.findViewById(R.id.itHappensItemLayout).setTag(io);
		TextView dateTextView = (TextView)v.findViewById(R.id.itHappensItemDateTextView);
		TextView textTextView = (TextView)v.findViewById(R.id.itHappensItemTextView);
		ImageView imageView = (ImageView)v.findViewById(R.id.itHappensItemThumbnailImageView);
		Media firstImageMedia = io.getFirstImageMedia();
		
		Bitmap gradientBmp = ViewUtils.getNewsItemGradientBitmap();
		
		BitmapDrawable bd = (BitmapDrawable)AppApplication.getAppContext().getResources().getDrawable(ResourceUtils.getNoImageResourceId());
		ImageCache.getInstance().loadImage(bd.getBitmap(), imageView, gradientBmp, 75, 80);
		
    	if (firstImageMedia!=null) {
    		ImageCache.getInstance().loadImage(firstImageMedia.getUrl(), imageView, gradientBmp, 75, 80);
    	}/* else {
    		BitmapDrawable bd = (BitmapDrawable)AppApplication.getAppContext().getResources().getDrawable(ResourceUtils.getNoImageResourceId());
    		ImageCache.getInstance().loadImage(bd.getBitmap(), imageView, gradientBmp, 75, 80);
    	}*/
    	
    	String headline = io.getHeadline(AppConfiguration.getInstance().getContentLanguage());
    	
    	if (headline==null || headline.length()<=0) {
    		headline = "Ingen overskrift";
    	}
		textTextView.setText(Html.fromHtml(headline));
    	dateTextView.setText(DateUtils.formatTime(io.getArrangementStartTime()));
				
		return v;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return infoObjects.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		observers.registerObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		observers.unregisterObserver(observer);
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int arg0) {
		return true;
	}
	
	public void clear() {
		infoObjects.clear();
		observers.notifyObservers();
	}
	
	public void addAll(List<InfoObject> infoObjects) {
		Collections.sort(infoObjects, new Comparator<InfoObject>() {
			public int compare(InfoObject lhs, InfoObject rhs) {
				return lhs.getArrangementStartTime().compareTo(rhs.getArrangementStartTime());
			}
		});
		this.infoObjects.addAll(infoObjects);
		observers.notifyObservers();
	}

}
